// 导入axios实例
import httpRequest from '../request/index';
// sha256加密
import sha256 from 'crypto-js/sha256';


// 获取电影全部年份
export function getYears() {
    return httpRequest({
        url: `/movie/years?time=${Math.round(new Date().getTime() / 1000)}`,
        method: 'get',
    });
}


// 获取全部电影
export function getAll() {
    return httpRequest({
        url: `/movie/all?time=${Math.round(new Date().getTime() / 1000)}`,
        method: 'get',
    });
}

// 获取全部电影（不分组）
export function getAllMovie(isAdmin = false) {
    if (isAdmin) {
        var header = { 'X-Admin-Req': 'Admin' }
    } else {
        var header = {}
    }
    return httpRequest({
        url: `/movie/listall?time=${Math.round(new Date().getTime() / 1000)}`,
        method: 'get',
        headers: header,
    });
}


// 分页获取电影列表数据
export function getList(pageSize, pageNum) {
    return httpRequest({
        url: `/movie?pageSize=${pageSize}&pageNum=${pageNum}&time=${Math.round(new Date().getTime() / 1000)}`,
        method: 'get',
    });
}


// 获取电影全部集数链接
export function getMovieLinks(id, isAdmin = false) {
    if (isAdmin) {
        var header = { 'X-Admin-Req': 'Admin' }
    } else {
        var header = {}
    }
    return httpRequest({
        url: `/movie/vlist?mid=${id}&time=${Math.round(new Date().getTime() / 1000)}`,
        method: 'get',
        headers: header,
    });
}


// 添加电影
export function addMovie(movie) {
    movie['time'] = Math.round(new Date().getTime() / 1000);
    let params = `${movie.name}${movie.url}${movie.category}${movie.year}XpFaEDYkQH`;
    movie['sign'] = sha256(params).toString().substring(0, 15);
    return httpRequest({
        baseURL: '/admin',
        url: `/movie/add`,
        method: 'post',
        data: movie,
        headers: { Authorization: `Bearer ${window.sessionStorage.getItem('token')}` }
    });
}


// 编辑电影
export function editMovie(id, movie) {
    movie['time'] = Math.round(new Date().getTime() / 1000);
    let params = `${movie.name}${movie.url}${movie.category}${movie.year}XpFaEDYkQH`;
    movie['sign'] = sha256(params).toString().substring(0, 15);
    return httpRequest({
        baseURL: '/admin',
        url: `/movie/edit/${id}`,
        method: 'put',
        data: movie,
        headers: { Authorization: `Bearer ${window.sessionStorage.getItem('token')}` }
    });
}


// 删除电影
export function deleteMovie(id) {
    let sign = sha256(id + 'XpFaEDYkQH').toString().substring(0, 15);
    return httpRequest({
        baseURL: '/admin',
        url: `/movie/${id}?time=${Math.round(new Date().getTime() / 1000)}&sign=${sign}`,
        method: 'delete',
        headers: { Authorization: `Bearer ${window.sessionStorage.getItem('token')}` }
    });
}



// 写评语
export function doMovieComment(id, comment) {
    comment['time'] = Math.round(new Date().getTime() / 1000);
    return httpRequest({
        baseURL: '/admin',
        url: `/movie/comment/${id}`,
        method: 'post',
        data: comment,
        headers: { Authorization: `Bearer ${window.sessionStorage.getItem('token')}` }
    });
}
